﻿using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Xml.Linq;
using iTransact.Requests;

namespace iTransact.Xml
{
	public static class XmlSegments
	{
		public static XElement CustomerData(Request request)
		{
			var customerData = new XElement("CustomerData");
			customerData.Add(new XElement("Email", request.Email));

			// Billing Address
			var billingAddress = new XElement("BillingAddress");
			billingAddress.Add(new XElement("Address1", request.BillingAddress));

			if (!string.IsNullOrEmpty(request.BillingAddress2))
				billingAddress.Add(new XElement("Address1", request.BillingAddress2));

			billingAddress.Add(new XElement("FirstName", request.BillingFirstName),
				new XElement("LastName", request.BillingLastName),
				new XElement("City", request.BillingCity),
				new XElement("State", request.BillingState),
				new XElement("Zip", request.BillingState),
				new XElement("Country", request.BillingCountry),
				new XElement("Phone", request.BillingPhone)
			);

			customerData.Add(billingAddress);

			if (!string.IsNullOrEmpty(request.ShippingAddress) && !string.IsNullOrEmpty(request.ShippingFirstName) || !string.IsNullOrEmpty(request.ShippingLastName))
			{
				// Shipping Address
				var shippingAddress = new XElement("ShippingAddress");
				shippingAddress.Add(new XElement("Address1", request.BillingAddress));

				if (!string.IsNullOrEmpty(request.ShipingAddress2))
					shippingAddress.Add(new XElement("Address2", request.ShipingAddress2));

				if (!string.IsNullOrEmpty(request.ShippingFirstName))
					shippingAddress.Add(new XElement("FirstName", request.ShippingFirstName));

				if (!string.IsNullOrEmpty(request.ShippingFirstName))
					shippingAddress.Add(new XElement("LastName", request.ShippingLastName));

				if (!string.IsNullOrEmpty(request.ShippingFirstName))
					shippingAddress.Add(new XElement("City", request.ShippingCity));
					if (!string.IsNullOrEmpty(request.ShippingState))
					shippingAddress.Add(new XElement("State", request.ShippingState));

				if (!string.IsNullOrEmpty(request.ShippingZip))
					shippingAddress.Add(new XElement("Zip", request.ShippingZip));
				
				if (!string.IsNullOrEmpty(request.ShippingCountry))
					shippingAddress.Add(new XElement("Country", request.ShippingCountry));

				if (!string.IsNullOrEmpty(request.ShippingCountry))
					shippingAddress.Add(new XElement("Phone", request.ShippingPhone));

				customerData.Add(shippingAddress);
			}

			if (!string.IsNullOrEmpty(request.CustomerId))
				customerData.Add(new XElement("CustId", request.CustomerId));

			return customerData;
		}

		public static XElement TotalsAndItems(Request request)
		{
			var orderItems = new XElement("OrderItems");
			if (request.OrderItems.Count > 0)
			{
				foreach (var orderItem in request.OrderItems)
				{
					orderItems.Add(new XElement("Item",
						new XElement("Description", orderItem.Description),
						new XElement("Cost", orderItem.Cost),
						new XElement("Qty", orderItem.Quantity))
					);
				}
			}
			else
			{
				orderItems.Add(new XElement("Total", request.Total));
				orderItems.Add(new XElement("Description", request.Description));
			}

			return orderItems;
		}

		public static XElement AccountInformation(Request request)
		{
			var accountInfo = new XElement("AccountInfo");

			accountInfo.Add(new XElement("CardAccount",
				new XElement("AccountNumber", request.CreditCardNumber),
				new XElement("ExpirationMonth", request.ExpirationMonth),
				new XElement("ExpirationYear", request.ExpirationYear),
				new XElement("CVVNumber", request.Cvv)
			));

			return accountInfo;
		}

		public static XElement RecurringData(ReocurringRequest request)
		{
			var recurringData = new XElement("RecurringData");
			recurringData.Add(
				new XElement("Recipe", request.Recipe),
				new XElement("RemReps", request.Repeats)
			);

			recurringData.Add(TotalsAndItems(request));
			return recurringData;
		}

		public static XElement TransactionControl(bool sendCustomerEmail, bool sendMerchantEmail, bool testMode, IEnumerable<string> emailLines)
		{
			var transactionControl = new XElement("TransactionControl");
			transactionControl.Add(new XElement("SendCustomerEmail", sendCustomerEmail ? "TRUE" : "FALSE"));
			transactionControl.Add(new XElement("SendMerchantEmail", sendMerchantEmail ? "TRUE" : "FALSE"));
			transactionControl.Add(new XElement("TestMode", testMode ? "TRUE" : "FALSE"));

			if (emailLines != null && emailLines.Count() > 0)
			{
				var emailText = new XElement("EmailText");
				foreach (var line in emailLines)
					emailText.Add(new XElement("EmailTextItem", line));

				transactionControl.Add(emailText);
			}

			return transactionControl;
		}

		public static XElement VendorData(Request request)
		{
			var vendorData = new XElement("VendorData");

			foreach (var key in request.MetaData.Keys)
			{
				vendorData.Add(new XElement("Element",
					new XElement("Name", key), new XElement("Value", request.MetaData[key]))
				);
			}

			return vendorData;
		}
	}
}
